package org.yanex.project.api.items;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class SelfItemAdapter extends BaseAdapter {
	
	private Context context;
	private List<? extends SelfItem> list;

	public Context getContext() {
		return context;
	}

	public int getCount() {
		if (list != null)
			return list.size();
		else
			return 0;
	}
	
	public SelfItem getItem(int position) {
		if (list != null)
			return list.get(position);
		else
			return null;
	}

	public long getItemId(int position) {
		return position;
	}

	public SelfItemAdapter(Context context, List<? extends SelfItem> list) {
		this.context = context;
		this.list = list;
	}
	
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		final SelfItem entry = list.get(position);

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(entry.getLayoutId(), null);
		}
		
		entry.setup(this, convertView, position);
		
		return convertView;
	}
}